<?php

Yii::import( "application.controllers.FrontendBaseController" );

class AlbumController extends FrontendBaseController
{
    public function actionIndex( $id )
    {   
        $album = $this->loadModel($id);
        $user = $this->getUserModel();
        $albumOwner = PhotoFacade::getUserByAlbum($album->album_id);
        $isOwner = $user->user_id == $albumOwner->user_id;
        
        if ($isOwner)
        {
            $this->breadcrumbs = array( "Главная" => "/",
                                    "Моя страница" => $this->getUserMainPageUrl(),
                                    "Фотоальбомы" => '/albums/' . $this->getUserIdFromSession(),
                                    $album->name    => '' );
        }
        else
        {
            $this->breadcrumbs = array( "Главная" => "/",
                                    "Моя страница" => $this->getUserMainPageUrl(),
                                    $albumOwner->surname . " " . $albumOwner->name => "/user/" . $albumOwner->user_id,
                                    "Фотоальбомы" => '/albums/' . $albumOwner->user_id,
                                    $album->name    => '' );
        }
        
                
        $photoCount = PhotoFacade::getCountPhotosByAlbum($id);
        $photos = PhotoFacade::getPhotosByAlbum($id);                        
        
        $this->render("album", array(
            'album'         => $album,
            'photos'        => $photos,
            'photoCount'    => $photoCount,
            'isOwner'       => $isOwner
        ));
        
    }
    
    public function actionAdd()
    {
        $user = $this->getUserModel();  
        
        $this->breadcrumbs = array( "Главная" => "/",
                                    "Моя страница" => $this->getUserMainPageUrl(),
                                    "Фотоальбомы"   => '/albums/' . $this->getUserIdFromSession(),
                                    "Создание фотоальбома" => "" );

        if (isset($_GET['success']))
        {
            $album = PhotoAlbum::model()->findByPk($_GET['success']);
            if ($album == null)
            {
                throw new CHttpException(404,'The requested page does not exist.');
            }
            $this->render('add', array(
                'success'   => true,
                'album'     => $album
                )
            );
            return;
        }
        if (!isset($_POST['PhotoAlbum']))
        {
            $model = new PhotoAlbum();
        
            $this->render('add', array('model' => $model));
        }
        else
        {
            $album = new PhotoAlbum();
            $album->attributes = $_POST['PhotoAlbum'];                        
            $album->save();
            
            if ($album->hasErrors())
            {
                $this->render('add', array('model' => $album));
            }
            else
            {
                $useralbum = new UserPhotoAlbum();
                $useralbum->user_id = $user->user_id;
                $useralbum->album_id = $album->album_id;
                $useralbum->save();

                $this->redirect('/album/add/success/' . $album->album_id);
            }                        
        }
        
    }
    
    public function actionEdit( $id )
    {
        $album = $this->loadModel($id);
        
        $this->breadcrumbs = array( "Главная" => "/",
                                    "Моя страница" => $this->getUserMainPageUrl(),
                                    "Фотоальбомы" => '/albums/' . $this->getUserIdFromSession(),
                                    $album->name    => '/album/' . $id,
                                    'Редактирование фотоальбома'    => '');
        
        if (!isset($_POST['PhotoAlbum']))
        {                    
            $this->render('edit', array('model' => $album));
        }
        else
        {            
            $album->attributes = $_POST['PhotoAlbum'];            
            $album->save();
            
            $this->redirect('/albums/' . $this->getUserIdFromSession());
        }        
    }
    
    public function actionAddPhoto( $id )
    {        
        $user = $this->getUserModel();
        $album = PhotoFacade::getPhotoAlbum($id);
        
        $this->breadcrumbs = array( "Главная" => "/",
                                    "Моя страница" => $this->getUserMainPageUrl(),
                                    "Фотоальбомы"   => '/albums/' . $this->getUserIdFromSession(),
                                    $album->name   => '/album/' . $id,
                                    "Загрузка фотографии" => "" );
        
        if ($album === null)
        {
            throw new CHttpException( 404, 'Страница не найдена' );
        }
        
        if (PhotoFacade::getUserByAlbum($album->album_id)->user_id != $user->user_id)
        {
            throw new CHttpException( 404, 'Страница не найдена' );
        }
        
        
        
        $this->render("addphoto", array(
            'album' => $album
        ));
    }
    
    public function actionDelete( $id )
    {
        $user = $this->getUserModel();
        $album = PhotoFacade::getPhotoAlbum($id);
        
        if ($album === null)
        {
            throw new CHttpException( 404, 'Альбом не найден' );
        }
        
        $albumOwner = PhotoFacade::getUserByAlbum($id);
        
        if ($user->user_id != $albumOwner->user_id)
        {
            throw new CHttpException( 500, 'Нет доступа' );
        }
        
        PhotoFacade::deleteAlbum($album);
        
        $this->redirect("/albums/" . $user->user_id);
    }
    
    private function loadModel($id)
    {
        $model = PhotoFacade::getPhotoAlbum($id);        
        if ($model === null)
        {
            throw new CHttpException( 404, 'Страница не найдена' );
        }
        return $model;
    }
    
    private function getUserModel($id = null)
    {
        if (!is_null($id))
        {
            $user = UserFacade::getUserById($id);
        }
        else
        {
            $user = UserFacade::getUserById($this->getUserIdFromSession());
        }
        if ($user === null)
        {
            throw new CHttpException( 404, 'The requested page does not exist.' );
        }
        return $user;
    }
    
}
?>
